
public var GamePaused = true;
public var Viseur:GUITexture;
public var Background:GameObject;
public var HUD:GUITexture;
public var Chat:GameObject;
public var MenuScript:GameObject;
public var Health:GUIText;
public var Jour:GUIText;
public var font:Font;
public var daynightcycle:GameObject;
private var ChatScript;
private var rctWindow:Rect = Rect((Screen.width/2)-200,(Screen.height/2)-100,400,300);
private var Pseudo:String = "";
private var JoinWindowActive = false;
private var ConnexionWindowActive = false;
private var CreateWindowActive = false;

var remoteIP:String = "0.0.0.0";
var remotePort:int = 4723;
var listenPort:int = 4723;
var yourIP:String = "";
var yourPort:String = "";

function Start()
{
	Viseur.enabled = false;
	Background.active = true;
	HUD.enabled = false;
	Health.enabled = false;
	Jour.enabled = false;
	Chat.active = false;
	MenuScript.active = false;
	daynightcycle.SetActiveRecursively(false);
	GamePaused = true;
}

function OnGUI () {
	GUI.skin.font = font;
	GUI.color = new Color(1,0.0,0.0,1);
	if(GamePaused){
		GUI.Box(Rect ((Screen.width/2)-200,(Screen.height/2)-100,400,300), "Menu principal");
		
		if(GUI.Button(new Rect((Screen.width/2)-100,(Screen.height/2)-20, 200, 40), "Rejoindre une partie"))
		{
			ToggleJoinWindow();
		}
		if(GUI.Button(new Rect((Screen.width/2)-100,(Screen.height/2)+40, 200, 40), "Creer une partie"))
		{
			ToggleCreateWindow();
		}
		if(GUI.Button(new Rect((Screen.width/2)-60,(Screen.height/2)+100, 120, 40), "Quitter"))
		{
			Application.Quit();
		}
		if(JoinWindowActive){
			rctWindow = GUI.Window(0, rctWindow, JoinWindow, "Rejoindre une partie", GUI.skin.GetStyle("window"));
		}
		if(ConnexionWindowActive){
			rctWindow = GUI.Window(1, rctWindow, ConnexionWindow, "Connexion...", GUI.skin.GetStyle("window"));
		}
		if(CreateWindowActive){
			rctWindow = GUI.Window(2, rctWindow, CreateWindow, "Créer une partie", GUI.skin.GetStyle("window"));
		}
	}
}


function OnConnectedToServer () {
	Chat.active = true;
	ChatScript = Chat.GetComponent("Chat");
	// Notify our objects that the level and the network are ready
	ChatScript.networkView.RPC("ApplyGlobalChatText", RPCMode.Others, Pseudo+" s'est connecté.", 1);
	for (var go : GameObject in FindObjectsOfType(GameObject))go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);  
}


function OnDisconnectedFromServer(info : NetworkDisconnection) {
    if (info == NetworkDisconnection.LostConnection)
    	ChatScript.networkView.RPC("ApplyGlobalChatText", RPCMode.Others, Pseudo+" a perdu la connexion.", 0);
    else
    	ChatScript.networkView.RPC("ApplyGlobalChatText", RPCMode.Others, Pseudo+" s'est déconnecté.", 0);
}   


function JoinWindow (windowID : int) {
    GUILayout.BeginVertical();
	// Checking if you are connected to the server or not
	if (Network.peerType == NetworkPeerType.Disconnected)
	{  
		// Fields to insert ip address and port 
		GUILayout.Label("IP du serveur :");
		remoteIP = GUILayout.TextField(remoteIP);
		GUILayout.Label("Port :");
		remotePort = int.Parse(GUILayout.TextField(remotePort.ToString()));
		GUILayout.Label("Nom du joueur :");
		Pseudo = GUILayout.TextField(Pseudo);
		GUILayout.Space(20);
		GUILayout.BeginHorizontal();
		if(Pseudo!=""){
			if (GUILayout.Button("Connexion",GUILayout.ExpandWidth(false)))
			{  
				gameObject.Find("PlayerInfo").GetComponent("PlayerInfo").SendMessage ("SetPseudo",Pseudo);
		 		// Connecting to the server
		 		Network.Connect(remoteIP, remotePort);
		 		ToggleJoinWindow();
		 		ToggleConnexionWindow();
			}  
		}
		if (GUILayout.Button("Annuler",GUILayout.ExpandWidth(false)))
		{  
	 		ToggleJoinWindow();
		}
		GUILayout.EndHorizontal();
	}  
	else
	{  		   
	  	 GUILayout.Label("Déjà connecté !");
	} 
		
	GUILayout.EndVertical();
}

function ConnexionWindow (windowID : int) {
    GUILayout.BeginVertical();
	// Checking if you are connected to the server or not
	if (Network.peerType == NetworkPeerType.Disconnected)
	{  
		GUILayout.Space(20);
		GUILayout.Label("Connexion à "+Network.player.externalIP+":"+Network.player.externalPort.ToString()+"...");
		if (GUILayout.Button("Annuler",GUILayout.ExpandWidth(false)))
		{  
			Network.Disconnect(200);
			ToggleConnexionWindow();
	 		ToggleJoinWindow();
		}
	}  
	else
	{  		   
		StartingGame();
		for (var go : GameObject in FindObjectsOfType(GameObject))
		{
			go.SendMessage("TogglePause", 
			SendMessageOptions.DontRequireReceiver); 
		}
		Viseur.enabled = true;
		Background.active = false;
		HUD.enabled = true;
		Health.enabled = true;
		Jour.enabled = true;
		MenuScript.active = true;
		daynightcycle.SetActiveRecursively(true);
		Screen.showCursor = false;
		Screen.lockCursor = true;
		ToggleConnexionWindow();
	} 	
	GUILayout.EndVertical();
}

function CreateWindow (windowID : int) {
    GUILayout.BeginVertical();
    GUILayout.Label("Nom du joueur :");
	Pseudo = GUILayout.TextField(Pseudo);
	if(Pseudo!=""){
		if (GUILayout.Button("Lancer la partie !",GUILayout.ExpandWidth(false)))
		{ 
			var playerInfoObj = GameObject.FindWithTag ("PlayerInfo");
			var playerInfoComp = playerInfoObj.GetComponent("PlayerInfo");
			playerInfoComp.SendMessage("SetPseudo",Pseudo);
			// Checking if you are connected to the server or not
			if (Network.peerType == NetworkPeerType.Disconnected)
			{  
				// Creating server
				Network.InitializeServer(64,listenPort,false);
				// Notify our objects that the level and the network is ready
				for (var go : GameObject in FindObjectsOfType(GameObject))
				{
					go.SendMessage("OnNetworkLoadedLevel", 
					SendMessageOptions.DontRequireReceiver); 
				}
			}  
		}
		if (Network.peerType == NetworkPeerType.Disconnected)
		{  

		}  
		else
		{  		   
			GUILayout.Space(20);
			GUILayout.Label("Serveur lancée sur "+Network.player.ipAddress+":"+Network.player.port.ToString()+".");
			if (GUILayout.Button("Stop",GUILayout.ExpandWidth(false)))
			{  
				Network.Disconnect(200);
				ToggleCreateWindow();
			}
			GUILayout.Space(20);
			GUILayout.Label("Appuyez sur une touche pour commencer...");
			GUI.UnfocusWindow();
			StartingGame();
			for (var go : GameObject in FindObjectsOfType(GameObject))
			{
				go.SendMessage("TogglePause", 
				SendMessageOptions.DontRequireReceiver); 
			}
			Viseur.enabled = true;
			Background.active = false;
			HUD.enabled = true;
			Health.enabled = true;
			Jour.enabled = true;
			Chat.active = true;
			ChatScript = Chat.GetComponent("Chat");
			MenuScript.active = true;
			daynightcycle.SetActiveRecursively(true);
			Screen.showCursor = false;
			Screen.lockCursor = true;
		}	
	}
	if (GUILayout.Button("Annuler",GUILayout.ExpandWidth(false)))
	{  
		ToggleCreateWindow();
	} 
	GUILayout.EndVertical();
}

function StartingGame()
{
	GamePaused = !GamePaused;
}


function Update(){
	if(Input.GetKeyDown(KeyCode.Tab)){
		
	}
}


function ToggleJoinWindow()
{
	JoinWindowActive = !JoinWindowActive;
}

function ToggleCreateWindow()
{
	CreateWindowActive = !CreateWindowActive;
}

function ToggleConnexionWindow()
{
	ConnexionWindowActive = !ConnexionWindowActive;
}